﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WinForms
{
    public partial class SettingsForm : Form
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private bool isChanged = false;

        public SettingsForm()
        {
            InitializeComponent();

        }

        private void findButton_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.CurrentDirectory + @"\DCDVBSource\";
            openFileDialog1.Filter = "dvb files (*.dvb)|*.dvb|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //try
                //{
                    graphTextBox.Text = openFileDialog1.FileName;
                    isChanged = true;

                //}
                //catch (Exception ex)
                //{
                //  log.Error("Can't find dvb file");
                //}
            }

        }

        private void SetFormUp()
        {
            isChanged = false;

            if (DisplayControl.Instance.DVBFile != null)
                graphTextBox.Text = DisplayControl.Instance.DVBFile.FullName;
            else
                graphTextBox.Text = "";

            if (DisplayControl.Instance.MHPDATAFolder != null)
                MHPTextBox.Text = DisplayControl.Instance.MHPDATAFolder.FullName;
            else
                MHPTextBox.Text = "";
        }

        private void SaveData()
        {
            try
            {
                FileInfo file = new FileInfo(graphTextBox.Text);

                if (file.Exists)
                {
                    DisplayControl.Instance.DVBFile = file;
                }
                else
                {
                    DisplayControl.Instance.DVBFile = null;
                    log.Error("DVB graph File does not exist");
                }
            }
            catch (Exception ex)
            {
                DisplayControl.Instance.DVBFile = null;
                log.Error("DVB graph File does not exist");
            }


            try
            {
                FileInfo file = new FileInfo(MHPTextBox.Text+"\\");

                if (file.Directory.Exists)
                {
                    DisplayControl.Instance.MHPDATAFolder = file;
                }
                else
                {
                    DisplayControl.Instance.MHPDATAFolder = null;
                    log.Error("MHPData Folder does not exist");
                }

            }
            catch (Exception ex)
            {
                DisplayControl.Instance.MHPDATAFolder = null;
                log.Error("DVB graph File does not exist");
            }

        }

        private void graphTextBox_TextChanged(object sender, EventArgs e)
        {
            isChanged = true;
        }

        private void oKButton_Click(object sender, EventArgs e)
        {
            if (isChanged)
                SaveData();

            this.Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            SetFormUp();
        }

        private void MHPFindButton_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.Description ="Select the MHPDATA directory.";

            // Do not allow the user to create new files via the FolderBrowserDialog.
            this.folderBrowserDialog1.ShowNewFolderButton = false;

            this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;

            // Show the FolderBrowserDialog.
            DialogResult result = folderBrowserDialog1.ShowDialog();
            
            if( result == DialogResult.OK )
            {
                MHPTextBox.Text = folderBrowserDialog1.SelectedPath;
                isChanged = true;
            }

            // Cancel button was pressed.
            else if (result == DialogResult.Cancel)
            {
                return;
            }


        }

        private void MHPTextBox_TextChanged(object sender, EventArgs e)
        {
            isChanged = true;
        }

    }
}
